package Data::Form::Field;
use strict;
use Carp;

my $CLASS_BASE = 'Data::Form::Field::';


sub make_field {
    my $class    = shift;
    my %params = @_;

	my $field_class;

	$field_class = $params{type} if defined $params{type};
	$field_class ||= defined $params{options} ? 'Option'	: 'Text';
	
	# If it doesn't look like a full package it's one of our base ones
	unless ( $field_class =~ /::/ ){
		$field_class = $CLASS_BASE . $field_class;
	}  
	
	# use the field module
	{
    	no strict 'refs';

    	eval "use $field_class";
    	croak "Can't locate $field_class in \@INC (\@INC contains: @INC)\n" if $@;
    	$field_class->import;
	}
	
	# instantiate the field object
	
    return $field_class->new(%params);
}

1;
__END__
=head1 NAME

Data::Form::Field - Factory for instaniating field objects

=head1 SYNOPSIS

	use Data::Form::Field;

    my $field = Data::Form::Field->make_field(
    			 name => 'username',
    			 required => 1
    );

=head1 DESCRIPTION

C<make_field> takes the input parameters and determines the class to which the arguments 
should be passed.

Here are the rules the C<type> attribute are evaluated against: 

=over 4

=item * does C<type> have a value   

=item * if it has :: then it is a user defined package

=item * if there is no :: it is one of the types defined in the distribution

=item * if it is a type from the distibution look for the C<options> attribute 
        and make it a Data::Form::Field::Option object

=back

A subclass of L<Data::Form::Field::Base> will be returned. 

=head1 SEE ALSO

=over 4

=item * L<Data::Form::Field::Base>   

=item * L<Data::Form::Field::Option>

=item * L<Data::Form::Field::Text>

=item * L<Data::Form::Field::Boolean>

=back 

=head1 AUTHOR

Clayton Scott, E<clscott@cpan.org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2007 by Clayton Scott

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut